import { Dictionary } from "../Support/Dictionary";
import { getQualifiedClassName } from "../Utility/dx/getQualifiedClassName";

export class InstanceManager
{
    private static _instance:InstanceManager = null;
    public static get instance():InstanceManager
    {
        if(!this._instance)this._instance=new InstanceManager();
        return this._instance;
    }
    private instanceDict:{[key:string]:any} = {};
    /**获取单例**/
    public getInstance(clz:any,instanceName?:any):any
    {
        var c:string = instanceName?instanceName:getQualifiedClassName(clz);
        if(!this.instanceDict[c])this.instanceDict[c] = new clz();//.add(c,new clz());//[c] = new clz();
        let o:any = this.instanceDict[c];
        if(instanceName&&o.hasOwnProperty("name"))o.name = instanceName;
        return o;
    }
    public hasInstance(clz:any,instanceName?:any):Boolean
    {
        var c:string = instanceName?instanceName:getQualifiedClassName(clz);
        return this.instanceDict[c]!=null;
    }
    public disposeInstance(clz:any,instanceName?:any):void
    {
        var c:string = instanceName?instanceName:getQualifiedClassName(clz);
        if(this.instanceDict[c])
        {
            let o:any = this.instanceDict[c];
            if(o&&o.hasOwnProperty("dispose"))o.dispose();
            delete this.instanceDict[c];
        }
    }
}